<template>
  <div class="add_coupons">
    <el-form :model="addForm" :rules="rules" ref="ruleForm" label-width="140px" label-position="right">
      <el-form-item label="优惠券名称:" prop="ticketName">
        <el-input v-model="addForm.ticketName" placeholder="请输入优惠券名称"></el-input>
      </el-form-item>
      <el-form-item label="使用门槛:" prop="minimumMoney">
        <el-radio-group v-model="radio" @change="changeMoney">
          <p style="margin: 10px 0;">
            <el-radio :label="3">无使用门槛</el-radio>
          </p>
          <el-radio :label="6">
            订单满
            <el-input :disabled="radio == 3" v-model="addForm.minimumMoney" size="mini" style="width: 50px;"></el-input> 元
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="优惠内容:" prop="preferentialType">
        <el-radio-group v-model="addForm.preferentialType" @change="changeType">
          <p style="margin: 10px 0;">
            <el-radio :label="1">
              满减券 减免
              <el-input :disabled="addForm.preferentialType == 2" v-model="addForm.offsetMoney" size="mini" style="width: 50px;"></el-input>元
            </el-radio>
          </p>
          <el-radio :label="2">
            折扣券 享受
            <el-input :disabled="addForm.preferentialType == 1" v-model="addForm.discountRate" size="mini" style="width: 50px;"></el-input> 折优惠
          </el-radio>
        </el-radio-group>
        <p class="tips" v-if="typetipShow">请输入优惠内容</p>
      </el-form-item>
      <el-form-item label="商品库存:" prop="stock">
        <el-input v-model="addForm.stock" placeholder="请输入商品库存"></el-input>
      </el-form-item>
      <el-form-item label="限领数量:" prop="limitQuantity">
        <el-radio-group v-model="addForm.isLimitQuantity" @change="changeQuantity">
          <p style="margin: 10px 0;">
            <el-radio :label="0">不限领</el-radio>
          </p>
          <el-radio :label="1">
            <el-input :disabled="addForm.isLimitQuantity == 0" v-model="addForm.limitQuantity" size="mini" style="width: 50px;"></el-input> 个/人
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="有效期:" prop="expiryDays">
        <el-radio-group v-model="addForm.isPermanent" @change="changePermanent">
          <p style="margin: 10px 0;">
            <el-radio :label="1">永久</el-radio>
          </p>
          <el-radio :label="0">
            <el-input :disabled="addForm.isPermanent == 1" v-model="addForm.expiryDays" size="mini" style="width: 50px;"></el-input> 天
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="宣传图片:" prop="imageList">
        <div class="uploadbtn" @click="uploadBtn">
          <input type="file" @change="upload" multiple ref="file" style="display: none;" />
          <img :src="addForm.imageList[0]">
          <i class="el-icon-upload" v-if="addForm.imageList[0].url == null" />
        </div>
      </el-form-item>
    </el-form>

    <p style="text-align: center;">
      <el-button @click="$emit('closeDialog')">取消</el-button>
      <el-button type="primary" @click="confirm('ruleForm')">创建优惠券</el-button>
    </p>



  </div>
</template>

<script>
  export default {
    props: ['id'],
    data() {
      return {
        radio: 3,
        typetipShow: false,
        addForm: {
          imageList: ['url:null'],
          minimumMoney: 0,
          preferentialType: 1,
          isLimitQuantity: 0,
          limitQuantity: 0,
          isPermanent: 1,
          expiryDays: 0
        },
        rules: {
          ticketName: { required: true, message: '请输入优惠券名称', trigger: 'blur' },
          contact: { required: true, message: '请输入联系人', trigger: 'blur' },
          stock: { required: true, message: '请输入商品库存', trigger: 'blur' },
          limitQuantity: { required: true, message: '请输入限购数量', trigger: 'blur' },
          expiryDays: { required: true, message: '请输入有效期', trigger: 'blur' },
          imageList: { required: true, message: '请上传宣传图片', trigger: 'blur' },
          minimumMoney: { required: true, message: '请填写满减金额', trigger: 'blur' }
        },
      }
    },

    mounted() {
      this.addForm.shopId = this.id;
    },

    methods: {
      confirm(formName) {
        if(this.addForm.isPermanent == 1) {
          this.addForm.expiryDays = 0
        }
        if(this.addForm.discountRate != null || this.addForm.offsetMoney != null) {
          this.typetipShow = false;
        } else {
          this.typetipShow = true;
        }
      },

      changeQuantity(e) {
        this.addForm.limitQuantity = e == 0 ? 0 : null
      },

      changeMoney(e) {
        this.addForm.minimumMoney = e == 3 ? 0 : ''
      },

      changePermanent(e) {
        this.addForm.expiryDays = e == 1 ? 0 : null
      },

      changeType(e) {
        if(e == 1) {
          this.addForm.discountRate = null;
        } else {
          this.addForm.offsetMoney = null;
        }
      },

      uploadBtn(){
        this.$refs.file.dispatchEvent(new MouseEvent('click'))
      },

      upload(e) {
        let files = e.target.files
        this.$nextTick(() => {
            // this.option.img = URL.createObjectURL(files[0]);
            // this.fileinfo = files[0].name;
            // this.dialogVisible = true
        })
        for (let i = 0; i < files.length; i++) {
          const isImg = files[i].type.indexOf('image') > -1
          const isLimit = files[i].size / 1024 / 1024 < 10

          if (!isImg) {
            this.$message.error('请上传图片格式!')
            return false
          }
          if (!isLimit) {
            this.$message.error('大小不能超过 10MB!')
            return false
          }
          let formData = new FormData()
          formData.append('files', files[i])

          var image = { url: null, percent: 0 }
          // this.imageList.push(image)

          this.uploadAxios(formData, image)
        }
      },

      uploadAxios(formData, image){
        this.$axios({
          url: 'https://file.zmzncs.com/upload/upload-batch',
          method: 'post',
          data: formData,
          timeout: 1000 * 60, // 请求超时时间
          onUploadProgress: (progress)=> {
            image.percent = Math.round(progress.loaded / progress.total * 100)
          }
        }).then((res) => {
          if (res.code == 200) {
            this.addForm.imageList = [];
            this.addForm.imageList.push(res.data[0].url)
            image.percent = 100
            image.url = res.data[0].url
            // this.imgs.push(image.url)
            //  组件返回值
            // var urlList = []
            // this.imgs = [];
            // this.imageList.forEach((image)=> {
            //   this.imgs.push(image.url)
            // })
            // var urls = urlList.toString()
            // this.$emit('input', urls)
          }
        }).catch(err => {})
      },
    }
  }
</script>

<style scoped>
  .el-input {
    width: 200px;
  }

  .uploadbtn {
    border: 1px solid #aaa;
    height: 100px;
    width: 100px;
    position: relative;
  }

  .uploadbtn img {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .el-icon-upload {
    font-size: 50px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .tips {
    padding: 0;
    margin: 0;
    color: red;
  }




</style>
